/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package httpserver.users;

import httpserver.connections.Connection;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Bryce
 */
public class User implements Runnable {

    private Connection connection;
    private boolean active = true;
    private String guid;

    public User(Socket socket) {
        guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
        connection = new Connection(socket, guid);
    }

    @Override
    public void run() {
        while (isActive()) {
            listen();
        }
    }
    //Listens for incomming data on user connection

    public void listen() {
        //Read the incomming data from the input stream
        ArrayList<String> data = connection.recData();
        if (data != null) {
            process(data);
        }
    }

    private void process(ArrayList<String> data) {
        System.out.println("Data:");
        for (String line : data) {
            System.out.println(line);
        }
        System.out.println("--end data--");

        //We have a websocket header, so parse + send response.
        if (data != null && data.size() > 0 && data.get(0).contains("HTTP") && data.get(1).equals("Upgrade: websocket")) {
            connection.setHost(data.get(3).split(":")[1].trim());
            connection.setKey(data.get(5).split(":")[1].trim());
            connection.setOrigin(data.get(4).split(":")[1].trim());
            connection.setWebsocketVersion(data.get(6).split(":")[1].trim());
            //Send response
            connection.acceptHandshake(data);
            //Send ack msg.
            connection.sendMsg("Testing testing");
        }
    }

    public boolean shutdown() {
        try {
            connection.getSocket().close();
        } catch (IOException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        String val = "User Created:\nGUID: " + guid + "\n" + "Req. Key: " + connection.getKey() + "\nIP: " + connection.getSocket().getInetAddress().getCanonicalHostName()
                + "\nPort: " + connection.getSocket().getPort() + "\n";
        if (connection.getWebsocketVersion() != null) {
            val += "WebsocketVersion: " + connection.getWebsocketVersion() + "\n";
        }
        if (connection.getHost() != null) {
            val += "Host:" + connection.getHost() + "\n";
        }
        if (connection.getOrigin() != null) {
            val += "Origin:" + connection.getOrigin() + "\n";
        }
        return val;
    }

    /**
     * @return the active
     */
    public boolean isActive() {
        return active;
    }

    /**
     * @param active the active to set
     */
    public void setActive(boolean active) {
        this.active = active;
    }

    /**
     * @return the guid
     */
    public String getGuid() {
        return guid;
    }

    /**
     * @param guid the guid to set
     */
    public void setGuid(String guid) {
        this.guid = guid;
    }
}
